//
//  ChooseCertificationStatusViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/15.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "ChooseCertificationStatusViewController.h"
#import "HttpRequest.h"
#import "RDVTabBarController.h"
#import "UIImageView+WebCache.h"
#import "AboutUsViewController.h"
#import "UIButton+WebCache.h"
#import <RongIMKit/RongIMKit.h>
#import "LoginCompanysViewController.h"
#import "UserIdentityCell.h"
@interface ChooseCertificationStatusViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate, UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate>

{
   
    NSMutableArray *userTypeNameArray;
    NSMutableArray *userTypeArray;
    NSMutableArray *imageUrlArray;
    UITableView*table;
}

@end

@implementation ChooseCertificationStatusViewController
@synthesize alt = alt;

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}

-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    self.view.backgroundColor=[UIColor whiteColor];
    CGRect backframe = CGRectMake(0,0,33,33);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
       self.title = @"选择认证的身份";
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [table removeFromSuperview];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    [table registerClass:[UserIdentityCell class] forCellReuseIdentifier:@"ChooseCertificationStatusViewController"];
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];

    
    
    UIView * tableFooterViews=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 400)];
    tableFooterViews.backgroundColor=zuiqianhuiColor;
    
    UIView*huiseview=[[UIView alloc]initWithFrame:CGRectMake(0,40, ScreenWidth, 50)];
    huiseview.backgroundColor=[UIColor whiteColor];
    huiseview.layer.borderWidth = 0.3;
    huiseview.layer.borderColor = qianhuiColor.CGColor;
    [tableFooterViews addSubview:huiseview];
    UIButton*exit=[[UIButton alloc]init];
    exit.backgroundColor=luseColor;
    [exit setTitle:@"立即认证" forState:UIControlStateNormal];
    exit. titleLabel.font=[UIFont systemFontOfSize:16];
    [exit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    exit.frame=CGRectMake(0, 0, ScreenWidth, 50);
    [exit addTarget:self action:@selector(exit:) forControlEvents:UIControlEventTouchUpInside];
    [huiseview addSubview:exit];
    table.tableFooterView = tableFooterViews;

    
    
    
    
    userTypeArray=[[NSMutableArray alloc]init];
    userTypeNameArray=[[NSMutableArray alloc]init];
    imageUrlArray=[[NSMutableArray alloc]init];
    self.userTypeName=@"";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.scId forKey:@"scId"];
    
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData1:@"Common/UserIdentity" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            
     
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                for (NSDictionary* dicc in [severDataDict.dataDict objectForKey:@"DataLine"] )
                {
                     [imageUrlArray addObject:[NSString stringWithFormat:@"%@",  [dicc valueForKey:@"imageUrl"]]];
                      [userTypeArray addObject:[dicc valueForKey:@"userType"]];
                        [userTypeNameArray addObject:[dicc valueForKey:@"userTypeName"]];
                }
                
                [table reloadData];
            
            
            }else{
                [Global hideProgressHUDInternal:self.view];
                  [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
                           
        }
        
        
        }else{
           
            [Global hideProgressHUDInternal:self.view];
           
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    
  
    
}
#pragma mark--TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return userTypeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



UserIdentityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseCertificationStatusViewController"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.titleImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[imageUrlArray objectAtIndex:indexPath.row]]] placeholderImage:[UIImage imageNamed:@"zhengfangxing"]];
cell.saveAccount=YES;
cell.title.text =[NSString stringWithFormat:@"%@",[userTypeNameArray objectAtIndex:indexPath.row]];
cell.title.font=[UIFont systemFontOfSize:15];
cell.title.textColor=qianheiColor;
cell.xuanzeimage.image=[UIImage imageNamed:@"quans"];
    
    
    
if  ([self.userTypeName isEqualToString:[userTypeNameArray objectAtIndex:indexPath.row]]){
    cell.xuanzeimage.image=[UIImage imageNamed:@"gous"];
    cell.saveAccount=NO;
}

    if (indexPath.row==0) {
                    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.40)];
                    lineLabel.backgroundColor=qianhuiColor;
                    [cell.contentView addSubview:lineLabel];
        
    }
        
if (userTypeNameArray.count !=indexPath.row+1) {
                    UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(50, 60-0.40, ScreenWidth-65, 0.40)];
                    lineLabel1.backgroundColor=qianhuiColor;
                    [cell.contentView addSubview:lineLabel1];
        
}
        
if (userTypeNameArray.count ==indexPath.row+1) {
                    UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 60-0.40, ScreenWidth, 0.40)];
                    lineLabel1.backgroundColor=qianhuiColor;
                    [cell.contentView addSubview:lineLabel1];
                    
    }
return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  60;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
        for (int i=0;  i<userTypeArray.count; i++) {
            NSIndexPath *indexPath1 =[NSIndexPath indexPathForRow:i inSection:0 ];
            UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath1];
            UserIdentityCell *sharecell=(UserIdentityCell *)cell;
            sharecell.xuanzeimage.image=[UIImage imageNamed:@"quans"];
        }
        UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        UserIdentityCell *sharecell=(UserIdentityCell *)cell;
        sharecell.xuanzeimage.image=[UIImage imageNamed:@"gous"];
    
        self.userTypeName=userTypeNameArray[indexPath.row];
        self.userType=userTypeArray[indexPath.row];
    
}

-(void)exit:(UIButton*)sender{
    
    
    if ([self.userTypeName length ]<1) {
        
        [self showAlertMessage:@"请选择认证身份！"];
   
    
    
    }else{
        [Global showIndeterminateHud:@"登录中，请稍后..." delay:30 view:self.view];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:Token forKey:@"tokenId"];
        [params setValue:self.scId forKey:@"scId"];
        [params setValue:self.userName forKey:@"userName"];
        [params setValue:self.sex forKey:@"sex"];
        [params setValue:self.userType forKey:@"userType"];
        [params setValue:self.userTypeName forKey:@"userTypeName"];
        
        
        [HttpRequest requestData1:@"Common/BindCompany" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    
                    [Global hideProgressHUDInternal:self.view];
                    NSLog(@"-------%@",severDataDict.dataDict);

                  
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeTokenId" object:nil];
                  
                    LoginCompanysViewController*de = [[LoginCompanysViewController alloc] init];
                    
                    [self.navigationController pushViewController:de animated:YES];
                    
                }else{
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:severDataDict.resultStr view:self.view];
            }
            
        }];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}
- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

@end
